package com.xu.drools.endpoint;

import com.xu.drools.bean.Student;
import com.xu.drools.bean.World;
import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.Multipart;
import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;

import javax.ws.rs.*;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author:whh
 * @date: 2025-08-15 23:35
 * <p></p>
 */
@Path("/services")
@Produces(MediaType.APPLICATION_JSON)
public class CommonService {




    @POST
    @Path("/student")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Student student(Student student){

        System.out.println(Thread.currentThread().getName());

        return student;
    }


    /**
     * json数据和xml数据都支持
     * {
     *     "a":1
     * }
     * <?xml version="1.0" encoding="UTF-8"?>
     * <world>
     *      <a>1</a>
     * </world>
     * @param world
     * @return
     */
    @POST
    @Path("/world")
    @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
    @Produces(MediaType.APPLICATION_XML)
    public World world(World world){
        return world;
    }



    @GET
    @Path("/download")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response download(@QueryParam("fileName") String fileName) throws UnsupportedEncodingException {
        if(StringUtils.isEmpty(fileName)){
            fileName="张三.docx";
        }

        File file = new File("/tmp/" + fileName);
        if (!file.exists()) {
            return Response.status(Response.Status.NOT_FOUND).entity("File not found").build();
        }

        StreamingOutput stream = output -> {
            try (InputStream input = new FileInputStream(file)) {
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = input.read(buffer)) != -1) {
                    output.write(buffer, 0, bytesRead);
                }
            }
        };
        return Response.ok(stream)
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") )
                .build();
    }





    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.TEXT_PLAIN)
    public  Response upload(@Multipart("files") List<Attachment> files) throws UnsupportedEncodingException {

        for (Attachment attachment : files) {
            String filename = attachment.getContentDisposition().getFilename();
            String decodedFilename = new String(filename.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
            File dest = new File("/tmp", decodedFilename);
            try {
                attachment.transferTo(dest);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        return Response.ok("success").build();
    }

}




